import time
from machine import Pin,I2C
import _thread
import bmp280
def run():
    i2c = I2C(sda=Pin(14), scl=Pin(13))
    b = bmp280.BMP280(i2c)
    da = 0
    t=0
    try:
        while(1):
            da=da+1
            f = open("date"+str('%06d' % da)+".txt",'r')
            f.close()
    except:
        f = open("date"+str('%06d' % da)+".txt",'a')
        f.write('气压计数据记录\n')
        f.write("记录时间:  原始温度和气压:  高度:\n")
        f.close()
    while(True):
        time.sleep_ms(100)
        f = open("date"+str('%06d' % da)+".txt",'a')
        t=t+1
        y = b.get()
        p = b.getPress()
        tp = b.getTemp()
        adjusted_pressure = p * (1.0 - 0.0065 * tp / 273.15) ** (-5.257)# 温度补偿
        h = ((101325 / adjusted_pressure) ** (1 / 5.257) - 1) * (tp + 273.15) / 0.0065# 根据修正后的气压计算高度
        dat = (str(t/10)+"  "+str(y)+"  "+str(h)+"\n")
        f.write(dat)
        #print(h)
        f.close()
pboot = Pin(9,Pin.IN)
def bled():
    led4 = Pin(12,Pin.OUT)
    led5 = Pin(13,Pin.OUT)
    while(True):
        led4.on()
        time.sleep_ms(500)
        led4.off()
        led5.on()
        time.sleep_ms(500)
        led5.off()
        #_thread.start_new_thread(bled,())
while(1):
    time.sleep_ms(100)
    if(pboot.value()==0):
        _thread.start_new_thread(bled,())
        run()
        break
